<%@ page language="java"
	import="java.util.*,cn.ebook.service.dao.impl.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE>
<html>
<head>
<base href="<%=basePath%>">
<title>首页</title>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link
	href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
	rel="stylesheet">
<script type="text/javascript" src="jQuery/jquery-3.3.1.js"></script>

<script type="text/javascript">



	function formSubmit() {
		var form = document.getElementById("indexForm");
		form.submit()

	}
	function aFormSubmit(nowPage) {
		document.getElementById("nowPage").value = nowPage;
		formSubmit();

	}
	function del(id, a) {
		if (confirm("是否确认删除该消息")) {

			$.ajax({
				url : "${pageContext.request.contextPath}/DelServlet",
				data : "id=" + id,
				type : "post",
				dateType : "json",
				success : function(result) {
					$(a).parent().parent().remove();
					alert(result.msg);
				},
				error : function(result) {
					alert(result.msg);
				}
			})

		}
	}
	function add() {
		var categoryid = $("#category").val();
		if (categoryid == 0) {
			alert("请您选择一个图书分类");

		} else {
			location.href = "${pageContext.request.contextPath}/add.jsp?categoryid=" + categoryid;
		}

	}
	function set(id) {
		//跳转到修改界面待着id
		location.href = "${pageContext.request.contextPath}/set.jsp?bookid=" + id;
	}
</script>

</head>

<body>
	<div class="container">

		<div class="row clearfix">
			<div class="col-md-12 column">
				<br> <br> <br>

				<form action="${Context.request.contextPath }/Ebookx/EbookServlet"
					method="get" id="indexForm">
					<input type="hidden" value="1" name="nowPage" id="nowPage">
					<span style="color: black;font-size: medium;margin-left: 400px">图书分类：</span>
					<select class="btn btn-default" name="categoryid" id="category">
						<option value="0">全部</option>
						<c:forEach items="${categorys }" var='category'>
							<option
								<c:if test="${categoryid==category.id }">selected='selected' </c:if>  value="${category.id }">${category.ebookCategory }
							</option>						
						</c:forEach>
					</select>
					
					<button type="button" class="btn btn-default btn-primary"
					onclick="formSubmit()">查询</button>
				<span style="margin-left: 400px"></span>
				<button type="button" class="btn btn-default btn-primary" onclick="add();">新增电子图书</button>
				</form>

				
				<table class="table table-bordered table-hover">
					<thead>
						<tr >
							<th style="text-align:center;">图书编号</th>
							<th style="text-align:center;">图书名称</th>
							<th style="text-align:center;">图书摘要</th>
							<th style="text-align:center;">上传人</th>
							<th style="text-align:center;">上传时间</th>
							<th style="text-align:center;">操作</th>
						</tr>
					</thead>
					<h3 style="text-align:center;">图书信息表</3>
					<tbody style="text-align:center;>
						<tr>
							<c:forEach items="${ebooks.list }" var="ebook">
								<tr class="info">
									<td>${ebook.id }</td>
									<td>${ebook.title }</td>
									<td>${ebook.summary }</td>									
									<td>${ebook.uploadUser }</td>
									<td>${ebook.createDate }</td>
									<td><a href="javaScript:void(0)" onclick="set(${ebook.id});">修改</a>&nbsp;&nbsp;&nbsp;<a href='javaScript:void(0)' onclick="del(${ebook.id},this)">删除</a></td>
								</tr>
							</c:forEach>
					</tbody>
				</table>
				<ul class="pagination">
					<span></span>

					<li><a href="javaScript:aFormSubmit(1);">首页</a></li>
					<c:if test="${ebooks.nowPage>1}">
					<li><a href="javaScript:aFormSubmit(${ebooks.nowPage-1});">上一页</a></li>
					</c:if>
					<c:if test="${ebooks.nowPage<ebooks.totalPage }">
					<li><a href="javaScript:aFormSubmit(${ebooks.nowPage+1});">下一页</a></li>
					</c:if>
					<li><a href="javaScript:aFormSubmit(${ebooks.totalPage });">尾页</a></li>
					<span style="font-size:24px;">共${ebooks.totalData }条资源- 当前页${ebooks.nowPage }/共${ebooks.totalPage }页-</span>
				</ul>
			</div>
		</div>
	</div>
</body>
</html>
